package com.woniuxy.productservice.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.status.CoachState;
import com.woniuxy.core.status.CourseTypeStateEnum;
import com.woniuxy.core.status.ProductState;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.productclient.client.CoachFeignClient;
import com.woniuxy.productclient.client.CourseFeignClient;
import com.woniuxy.productclient.client.dto.CoachInfoDto;
import com.woniuxy.productclient.client.dto.CourseTypeDto;
import com.woniuxy.productdao.mapper.CourseProductMapper;
import com.woniuxy.productdao.model.AloneCourse;
import com.woniuxy.productdao.model.CourseProduct;
import com.woniuxy.productmodel.dto.AloneCourseProductDto;
import com.woniuxy.productmodel.dto.CourseProductDto;
import com.woniuxy.productmodel.form.AddCourseProductForm;
import com.woniuxy.productmodel.form.ListCourseProductForm;
import com.woniuxy.productmodel.form.UpdateCourseProductForm;
import com.woniuxy.productservice.exception.ProductException;
import com.woniuxy.productservice.service.CourseProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */

@Service
public class CourseProductServiceImpl extends ServiceImpl<CourseProductMapper, CourseProduct> implements CourseProductService {

    @Autowired
    private CoachFeignClient coachFeignClient;
    @Autowired
    private CourseFeignClient courseFeignClient;

    /**
     * 新增普通课程产品
     * @param form
     * @return
     */
    @Override
    public CourseProductDto addCourseProduct(AddCourseProductForm form,Integer venueId) {
        Result<CoachInfoDto> res1 = coachFeignClient.findCoachByCoachId(form.getCoachId());
        if (!res1.getFlag()) throw new ProductException(res1.getCode(),res1.getMsg());
        CoachInfoDto coachInfo = res1.getData();
        if (ObjectUtils.isEmpty(coachInfo)) throw new ProductException(50001,"教练不存在");
        if (venueId!=coachInfo.getVenueId()) throw new ProductException(50002,"教练不属于该场馆");
        if(coachInfo.getCoachState()!= CoachState.IS_ENABLE.getState()) throw new ProductException(50005,"教练未启用");
        //判断课程类别是否存在
        Result<CourseTypeDto> res2 = courseFeignClient.findCourseTypeById(form.getCourseTypeId());
        if (!res2.getFlag()) throw new ProductException(res2.getCode(),res2.getMsg());
        CourseTypeDto courseType = res2.getData();
        if (ObjectUtils.isEmpty(courseType)) throw new ProductException(50006,"该课程类别不存在");
        if (courseType.getCourseTypeState()!= CourseTypeStateEnum.TYPE_ENABLE.getCode()) throw new ProductException(50007,"课程类别未启用");
        //新增产品
        CourseProduct courseProduct = BeanUtil.toBean(form, CourseProduct.class);
        courseProduct.setCourseProductState(ProductState.IS_ENABLE.getState());
        courseProduct.setVenueId(venueId);
        courseProduct.setCoachId(coachInfo.getCoachId());
        courseProduct.setCoachName(coachInfo.getCoachName());
        courseProduct.setCoachDesc(coachInfo.getCoachRemark());
        courseProduct.setCourseTypeId(courseType.getCourseTypeId());
        courseProduct.setCourseTypeName(courseType.getCourseTypeName());
        save(courseProduct);
        CourseProductDto dto = BeanUtil.toBean(courseProduct, CourseProductDto.class);
        return dto;
    }

    /**
     * 商家端启用普通课程产品
     * @param courseProductId
     * @param venueId
     */
    @Override
    public void enableCourseProduct(Integer courseProductId, Integer venueId) {
        CourseProduct courseProduct = getById(courseProductId);
        if (ObjectUtils.isEmpty(courseProduct)) throw new ProductException(60001,"产品不存在");
        if (courseProduct.getVenueId()!=venueId) throw new ProductException(60002,"不是该商家的产品");
        if (courseProduct.getCourseProductState()==ProductState.IS_ENABLE.getState())
            throw new ProductException(60003,"已启用，重复操作");
        courseProduct.setCourseProductState(ProductState.IS_ENABLE.getState());
        updateById(courseProduct);
    }

    @Override
    public void disabledCourseProduct(Integer courseProductId, Integer venueId) {
        CourseProduct courseProduct = getById(courseProductId);
        if (ObjectUtils.isEmpty(courseProduct)) throw new ProductException(60001,"产品不存在");
        if (courseProduct.getVenueId()!=venueId) throw new ProductException(60002,"不是该商家的产品");
        if (courseProduct.getCourseProductState()==ProductState.IS_DISABLED.getState())
            throw new ProductException(60003,"已禁用，重复操作");
        courseProduct.setCourseProductState(ProductState.IS_DISABLED.getState());
        updateById(courseProduct);
    }

    /**
     * 商家修改普通课程产品信息
     * @param form
     * @param venueId
     */
    @Override
    public void updateCourseProduct(UpdateCourseProductForm form, Integer venueId) {
        CourseProduct courseProduct = getById(form.getCourseProductId());
        if (ObjectUtils.isEmpty(courseProduct)) throw new ProductException(60001,"产品不存在");
        if (courseProduct.getVenueId()!=venueId) throw new ProductException(60002,"不是该商家的产品");
        Result<CoachInfoDto> res1 = coachFeignClient.findCoachByCoachId(form.getCoachId());
        if (!res1.getFlag()) throw new ProductException(res1.getCode(),res1.getMsg());
        CoachInfoDto coachInfo = res1.getData();
        if (ObjectUtils.isEmpty(coachInfo)) throw new ProductException(50001,"教练不存在");
        if (venueId!=coachInfo.getVenueId()) throw new ProductException(50002,"教练不属于该场馆");
        if(coachInfo.getCoachState()!= CoachState.IS_ENABLE.getState()) throw new ProductException(50005,"教练未启用");
        //判断课程类别是否存在
        Result<CourseTypeDto> res2 = courseFeignClient.findCourseTypeById(form.getCourseTypeId());
        if (!res2.getFlag()) throw new ProductException(res2.getCode(),res2.getMsg());
        CourseTypeDto courseType = res2.getData();
        if (ObjectUtils.isEmpty(courseType)) throw new ProductException(50006,"该课程类别不存在");
        if (courseType.getCourseTypeState()!= CourseTypeStateEnum.TYPE_ENABLE.getCode()) throw new ProductException(50007,"课程类别未启用");
        //修改产品
        courseProduct.setCoachId(form.getCoachId());
        courseProduct.setCoachName(coachInfo.getCoachName());
        courseProduct.setCoachDesc(coachInfo.getCoachRemark());
        courseProduct.setCourseTypeId(form.getCourseTypeId());
        courseProduct.setCourseTypeName(courseType.getCourseTypeName());
        courseProduct.setCourseProductName(form.getCourseProductName());
        courseProduct.setCourseDesc(form.getCourseDesc());
        updateById(courseProduct);
    }

    /**
     * 条件查询普通课程产品信息
     * @param form
     * @return
     */
    @Override
    public PageInfo<CourseProductDto> listCourseProduct(ListCourseProductForm form) {
        Long current = form.getCurrent();
        Long size = form.getSize();
        Page<CourseProduct> page = new Page<>(current == null ? 1l : current, size == null ? 6l : size);
        LambdaQueryWrapper<CourseProduct> wrapper = new LambdaQueryWrapper<CourseProduct>()
                .eq(!ObjectUtils.isEmpty(form.getVenueId()), CourseProduct::getVenueId, form.getVenueId())
                .eq(!ObjectUtils.isEmpty(form.getCourseTypeId()), CourseProduct::getCourseTypeId, form.getCourseTypeId())
                .like(!ObjectUtils.isEmpty(form.getCourseProductName()), CourseProduct::getCourseProductName, form.getCourseProductName())
                .orderByAsc(CourseProduct::getCourseProductId);
        page(page,wrapper);
        List<CourseProductDto> dtos = BeanUtil.copyToList(page.getRecords(), CourseProductDto.class);
        PageInfo<CourseProductDto> pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(dtos);
        return pageInfo;
    }
}
